﻿using System.Collections.ObjectModel;
using MedicineTracker.Model;
using System.ComponentModel;
using System;
using System.Windows.Data;

namespace MedicineTracker.ViewModel
{
    public class MedicinesViewModel : Database, INotifyPropertyChanged
    {
        private ObservableCollection<Medicine> _medicines;

        public ObservableCollection<Medicine> Medicines
        {
            get { return _medicines; }
        }

        public MedicinesViewModel()
        {
            _medicines = new ObservableCollection<Medicine>();
            using (Connection)
            {
                try
                {
                    Connection.Open();
                    Command.CommandText = "SELECT * FROM medicines";
                    Command.Connection = Connection;
                    DataReader = Command.ExecuteReader();
                    while (DataReader.Read())
                    {
                        _medicines.Add(new Medicine
                        {
                            ID = Convert.ToInt32(DataReader["id"]),
                            Name = (string)DataReader["name"],
                            MilkWP = Convert.ToInt32(DataReader["milk_withdrawal_period"]),
                            MeatWP = Convert.ToInt32(DataReader["meat_withdrawal_period"])
                        });
                    }
                }
                catch
                {
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}